;
var helper = require("../../javascript/helper");
var request = require("request");
var db = require("../../javascript/mysql");
var mysql = new db.mysqlClient("accident");
var spider = function () {
    let that = this;
    this.name = "户外活动意外险"
    this.genProductInfo = function () {
        let projects = [];
        plan = {
            planA: {
                "year": ["6-65"],
                "days": ["1d", "30d", "365d"],
                "people_count": ["1"],
                "accident_death": ["100000"],
                "accident_medical": ["20000"],
                "exigency_back": ["10000"],
                "sudden_death": ["5000"],
                "accident_ambulance": ["500"]
            },
            planB: {
                "year": ["6-65"],
                "days": ["1d", "30d", "365d"],
                "people_count": ["1"],
                "accident_death": ["200000"],
                "accident_medical": ["50000"],
                "exigency_back": ["50000"],
                "sudden_death": ["10000"],
                "accident_ambulance": ["500"]
            }
        }
        for (k in plan) {
            data = helper.product([
                plan[k]["year"], plan[k]["days"], plan[k]["people_count"], plan[k]["accident_death"], plan[k]["accident_medical"], plan[k]["exigency_back"], plan[k]["sudden_death"], plan[k]["accident_ambulance"]
            ])
            for (i = 0; i < data.length; i++) {
                dt = helper.formatProductInfo(data[i], plan[k]);
                projects.push(dt);
            }
        }
        return projects;
    }
    this.httpRequest = function (dt) {
        if (dt.days == "1d") {
            date = "日保费"
        } else if (dt.days == "30d") {
            date = "月保费"
        } else {
            date = "一年期"
        }
        return new Promise(function (resolve, rejects) {
            params = '{"planBaseInfo":{"productCode":"0615mm","planCode":"","cityCode":"10000000","channelCode":"","requestType":"","calculateParameterList":[{"parameterCode":"visitCountryNum","value":"' + date + '"}],"kindList":[],"count":"1"},"head":{"uuid":"1234","tonken":"test","channelNo":"test","checkStr":"test"}}ss'
            request({
                url: "http://weix.generali-china.cn/mapis/zuul-pageInit/pageInit/plan",
                method: "POST",
                json: false,
                rejectUnauthorized: false,
                headers: {
                    "Host": "weix.generali-china.cn",
                    "Accept": "application/json, text/plain, */*",
                    "Accept-Language": "zh-cn",
                    "Content-Type": "application/json;charset=utf-8",
                    "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/7.0.12(0x17000c30) NetType/4G Language/zh_CN",
                    "Connection": "keep-alive",
                    "Cookie": "jsapi_ticket=sM4AOVdWfPE4DxkXGEs8VFk_iFpS8kYgck6f3NX7lN_Qa9HIdOLxHkn93P0qitdIztVb4cdCdJdAmSglmZangA"

                },
                body: (params)

            }, function (err, res, body) {

                if (!err && res.statusCode == 200) {
                    // console.log( params )
                    // console.log( body );
                    if (JSON.parse(body)["planList"].length != 0) {
                        if (dt.accident_death == "100000") {
                            price = JSON.parse(body)["planList"][0]["premium"].toFixed(2).toString();
                        } else {
                            price = JSON.parse(body)["planList"][1]["premium"].toFixed(2).toString();

                        }
                        rs = {
                            name: that.name,
                            company: "中意财险",
                            price: price
                        }
                        resolve(rs);

                    }

                }

            })

        })

    }
    this.main = async function () {
        data = that.genProductInfo();
        
        for (i = 0; i < data.length; i++) {
            dt = data[i];
            let rs = await that.httpRequest(dt);
            result = Object.assign( dt,rs );
            result.sports = "'" + '["游泳","登山","漂流","潜水","自行车运动","骑马","滑雪","野营"]' + "'"
            let Sql = 'INSERT INTO `accident`(`name`,`company`,`year`,`days`,`people_count`,`accident_death`,`accident_medical`,`exigency_back`,`sudden_death`,`accident_ambulance`,`sports`,`price`)VALUES("'+result.name+'","'+result.company+'","'+result.year+'","'+result.days+'","'+result.people_count+'","'+result.accident_death+'","'+result.accident_medical+'","'+result.exigency_back+'","'+result.sudden_death+'","'+result.accident_ambulance+'",'+result.sports+',"'+result.price+'")'
            mysql.db.query(Sql, function( err,data){
                if(!err){
                    console.log( Sql );
                }
                else{
                    console.log("写入数据库失败")
                }
            })
        }
        mysql.close();
    }

}
